<template>
	<a-drawer
		:title="formData.id ? '编辑外部人员' : '增加外部人员'"
		:width="620"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="24">
					<a-form-item label="账号：" name="account">
						<a-input v-model:value.trim="formData.account" placeholder="请输入账号" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="姓名：" name="name">
						<a-input v-model:value.trim="formData.name" placeholder="请输入姓名" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="卡号：" name="userNo">
						<a-input v-model:value.trim="formData.userNo" placeholder="请输入卡号" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="工作单位：" name="orgId">
						<a-tree-select
							v-model:value="formData.orgId"
							style="width: 100%"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							placeholder="请选择工作单位"
							allow-clear
							tree-default-expand-all
							:tree-data="treeData"
							:tree-default-expanded-keys="treeDefaultExpandedKeys"
							:field-names="{
										children: 'children',
										label: 'name',
										value: 'id'
									}"
						></a-tree-select>
					</a-form-item>
				</a-col>

				<a-col :span="12">
					<a-form-item label="手机号码：" name="phone">
						<a-input v-model:value.trim="formData.phone" placeholder="请输入手机号码" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="手机已验证：" name="phoneVerified">
						<a-switch
							v-model:checked="phoneVerified"
							checked-children="是" un-checked-children="否"
							:disabled="!formData.phone"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="邮箱：" name="email">
						<a-input v-model:value.trim="formData.email" placeholder="请输入邮箱" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="邮箱已验证：" name="emailVerified">
						<a-switch
							v-model:checked="emailVerified"
							checked-children="是" un-checked-children="否"
							:disabled="!formData.email"
						/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="formLoading" @click="onSubmit">保存</a-button>
		</template>
	</a-drawer>
</template>

<script setup>
	import bizUserApi from '@/api/biz/bizUserApi'
	import {required} from '@/utils/formRules'
	import { message } from "ant-design-vue";
	// 默认是关闭状态
	let visible = $ref(false)
	const formRef = ref()
	const {proxy} = getCurrentInstance()
	const emit = defineEmits({successful: null})
	const formLoading = ref(false)
	const treeData = ref([])
	const treeDefaultExpandedKeys = ref([])
	const phoneVerified = ref(false)
	const emailVerified = ref(false)
	// 表单数据
	let formData = ref({})

	// 打开抽屉
	const onOpen = (record) => {
		visible = true
		if (record) {
			convertFormData(record)
		}
		// 机构选择器数据
		bizUserApi.userOrgTreeSelector({
			orgType: 1
		}).then((res) => {
			if (res !== null) {
				treeData.value = res
				// 默认展开2级
				treeData.value.forEach((item) => {
					// 因为0的顶级
					if (item.parentId === '0') {
						treeDefaultExpandedKeys.value.push(item.id)
						// 取到下级ID
						if (item.children) {
							item.children.forEach((items) => {
								treeDefaultExpandedKeys.value.push(items.id)
							})
						}
					}
				})
			}
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		treeData.value = []
		treeDefaultExpandedKeys.value = []
		visible = false
		phoneVerified.value = false
		emailVerified.value = false
	}
	// 回显数据
	const convertFormData = (record) => {
		const param = {
			id: record.id
		}
		// 查询详情
		bizUserApi.userDetail(param).then((data) => {
			if (data.phoneVerified === 1) {
				phoneVerified.value = true
			} else {
				phoneVerified.value = false
			}
			if (data.emailVerified === 1) {
				emailVerified.value = true
			} else {
				emailVerified.value = false
			}
			formData.value = Object.assign(formData.value, data)
		})
	}
	// 默认要校验的
	const formRules = {
		account: [required('请输入账号')],
		name: [required('请输入姓名')],
		orgId: [required('请选择组织')],
		userType: [required('请选择类型')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			// 因为不切断，我下面转换数据格式，影响上面表单会报错
			let formDatas = JSON.parse(JSON.stringify(formData.value))
			formDatas.phoneVerified = phoneVerified.value ? 1 : 0
			formDatas.emailVerified = emailVerified.value ? 1 : 0
			formDatas.userType = 3
			formLoading.value = true
			bizUserApi
				.submitForm(formDatas, !formDatas.id)
				.then(() => {
					if(formDatas.id){
						message.success('编辑成功')
					}else{
						message.success('新增成功')
					}
					onClose()
					emit('successful')
				})
				.finally(() => {
					formLoading.value = false
				})
		})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>

<style scoped type="less">
	.childAddButton {
		margin-bottom: 10px;
	}

	.form-row {
		background-color: var(--item-hover-bg);
		margin-left: 0px !important;
	}

	.form-row-con {
		padding-bottom: 5px;
		padding-top: 5px;
		padding-left: 15px;
	}

	.dashedButton {
		margin-top: 10px;
		width: 100%;
	}

	.form-div {
		padding-top: 10px;
	}
</style>
